<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity {
	private $_id;
	const ERROR_ISNOT_ADMIN = 300;
	const ERROR_DISABLED = 400;
	
	/**
	 * Authenticates a user.
	 * @return boolean whether authentication succeeds.
	 */
	public function authenticate() {
        $user = ForumMembers::model()->findByUserName($this->username);
        $this->_id = $user->uid;
        $this->username = $user->username;
		return true;
	}

	public function authenticateAdmin() {
		$user = Users::model()->find('userEmail = ?', array($this->username));
		if($user === null) {
			$this->errorCode = self::ERROR_USERNAME_INVALID;
		} else if(md5($this->password) !== $user->userPassword) {
			$this->errorCode = self::ERROR_PASSWORD_INVALID;
		} else if($user->userRole != Users::ROLE_ADMIN) {
			$this->errorCode = self::ERROR_ISNOT_ADMIN;
		} else {
			$this->_id = $user->userId;
			Yii::app()->user->setState('userRole', $user->userRole);
			$this->username = $user->userEmail;
			$this->errorCode = self::ERROR_NONE;
		}
		return ! $this->errorCode;
	}
	
	/**
	 * @return integer the ID of the user record
	 */
	public function getId() {
		return $this->_id;
	}
}